/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : RoundingProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Nov 14, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.math.number;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.number.util.NumberFormatter;
import net.lwf.number.util.NumberRounder;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;




public class RoundingProcessHandler extends ProcessHandler implements IProcessHandler {

	private String TENS_KEY = "10";
	private String HUNDREDS_KEY = "100";
	private String THOUSANDS_KEY = "1000";
	private String TEN_THOUSANDS_KEY = "10000";
	
	private int NUMBER_START_RANGE = 1;
	private int NUMBER_END_RANGE = 99999;
	
	private String grade;
	
	public RoundingProcessHandler(String grade) {
		this.grade = grade;
	}
	
	public DisplayFormatter getDisplayFormatter() {
		
		return null;
	}

	public String getQuestionDesc() {
		
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		QuestionInfo qinfo = new QuestionInfo();
		int number = RandomNumberGenerator.generate(NUMBER_START_RANGE, NUMBER_END_RANGE);
		Map<String, String> mapdata = NumberRounder.getRoundedData(number);
		
		int roundneeded = RandomNumberGenerator.generate(1,4);
		
		while(roundneeded > mapdata.size()) {
			roundneeded = RandomNumberGenerator.generate(1,4);
		}
		
		String roundednum = null;
		String roundedTo = null;
		if(roundneeded == 2) { //Tens
			roundednum = (String) mapdata.get(HUNDREDS_KEY);
			roundedTo = "Hundreds";
		}
		else if(roundneeded == 3) { //Tens
			roundednum = (String) mapdata.get(THOUSANDS_KEY);
			roundedTo = "Thousands";
		}
		else if(roundneeded == 4) { //Tens
			roundednum = (String) mapdata.get(TEN_THOUSANDS_KEY);
			roundedTo = "Ten Thousands";
		}
		else {
			roundednum = (String) mapdata.get(TENS_KEY);
			roundedTo = "Tens";
		}
		qinfo.setAnswer(roundednum);
		qinfo.setQuestion(NumberFormatter.format(String.valueOf(number)));
		qinfo.setPlaceHolder("Round the following to the next " + roundedTo); //For instructional text on the UI

		return qinfo;
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle();
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
		
		
	}

	@Override
	public int getRows() {
		
		return 10;
	}

	@Override
	public int getColumns() {
		
		return 3;
	}

	@Override
	public boolean isPortrait() {
		
		return true;
	}

	@Override
	public String getPdfInstructionalText() {
		
		return null;
	}

	@Override
	public String getPdfNote() {
		
		return null;
	}
	
}


